/*
 * 工具函数封装
 *
 */

const showMassage = (message: string, type: any) => {
  if (type == "error") {
    ElMessage.error(message);
  } else {
    ElMessage({ message, type });
  }
};

import { menuinfo, User } from "@/types/type";
// 封装token
import { useCookies } from "@vueuse/integrations/useCookies";
const cookie = useCookies();
const Cookies = {
  setToken: (data: string) => cookie.set("token", data),
  getToken: () => cookie.get("token"),
  removeToken: () => cookie.remove("token"),
};

// 格式化时间
const formateDate = (date: any, rule?: any) => {
  let fmt = rule || "yyyy-MM-dd hh:mm:ss";
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, date.getFullYear());
  }
  const o: any = {
    // 'y+': date.getFullYear(),
    "M+": date.getMonth() + 1,
    "d+": date.getDate(),
    "h+": date.getHours(),
    "m+": date.getMinutes(),
    "s+": date.getSeconds(),
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      const val = o[k] + "";
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length == 1 ? val : ("00" + val).substr(val.length)
      );
    }
  }
  return fmt;
};

// 分类菜单栏
function classifyMenu(newarr: User[], arr: User[], type?: string) {
  arr.forEach((item: User) => {
    if (item.parentId[0] === null && item.menuType == 1) {
      if (type && item.menuType == 1) {
        newarr.push(item);
      } else {
        newarr.push(item);
      }
    }
    for (const key in newarr) {
      newarr[key]["children"] = [];
      for (let v of arr) {
        if (v.parentId.length == 1 && newarr[key]._id == v.parentId[0]) {
          newarr[key].children.push({ ...v });
        }
        if (v.parentId.length > 1) {
          for (let subval of newarr[key]["children"]) {
            subval["children"] = [];
            for (const v1 of arr) {
              if (subval._id == v1.parentId[v.parentId.length - 1]) {
                subval.children.push({ ...v1 });
                subval.action = subval.children;
              }
            }
          }
        }
      }
    }
  });
}
// 深度优先遍历
function Getpermission(arr: menuinfo[]) {
  let newarr: any = {};
  const deep = (arr: menuinfo[]) => {
    while (arr.length) {
      let item = arr.pop();
      if (item!.action && item!.children) {
        newarr[item!._id] = item!.menuName;
      }
      if (item!.children) {
        deep(item!.children);
      }
    }
  };
  deep(JSON.parse(JSON.stringify(arr)));
  return newarr;
}

// 消息弹出框
import type { Action } from "element-plus";
const ShowElMessageBox = (
  msg: string,
  message: string,
  fn?: any,
  callback?:any
) => {
  ElMessageBox.confirm(msg, "消息确认", {
    distinguishCancelAndClose: true,
    confirmButtonText: "确认",
    cancelButtonText: "取消",
  })
    .then(() => {
      console.log('111111');
      fn();
      callback()
      ElMessage({
        type: "success",
        message,
      });

    })
    .catch((action: Action) => {});
};
export {
  showMassage,
  Cookies,
  formateDate,
  classifyMenu,
  Getpermission,
  ShowElMessageBox,
};
